package com.redstars.generate.service.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONString;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.service.SysGenerateTemplateService;
import com.redstars.generate.utils.PageUtils;
import lombok.AllArgsConstructor;
import com.redstars.generate.convert.SysTemplateVersionConvert;
import com.redstars.generate.entity.SysTemplateVersion;
import com.redstars.generate.query.SysTemplateVersionQuery;
import com.redstars.generate.vo.SysTemplateVersionVo;
import com.redstars.generate.dao.SysTemplateVersionDao;
import com.redstars.generate.service.SysTemplateVersionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

import javax.annotation.Resource;

/**
 * 模板版本
 *
 * @author zhouhx 
 * @since 1.0.0 2022-10-26
 */
@Service
@AllArgsConstructor
public class SysTemplateVersionServiceImpl extends ServiceImpl<SysTemplateVersionDao, SysTemplateVersion> implements SysTemplateVersionService {
   @Resource
    SysGenerateTemplateService templateService;
    @Override
    public void save(SysTemplateVersionVo vo) {
        SysTemplateVersion entity = SysTemplateVersionConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);

    }

    @Override
    public void update(SysTemplateVersionVo vo) {
        SysTemplateVersion entity = SysTemplateVersionConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        int i = baseMapper.deleteBatchIds(idList);
        if(i<0){
            throw  new RedstarsException(String.format("删除模板版本失败,ids:%s", JSONUtil.toJsonStr(idList)));
        }
        idList.forEach(id ->{
            templateService.removeByTemplateVersion(id);
        });
    }

    @Override
    public PageResult<SysTemplateVersionVo> page(SysTemplateVersionQuery query) {
        LambdaQueryWrapper<SysTemplateVersion> wrapper = getWrapper(query);
        PageUtils<SysTemplateVersion> pageUtils = new PageUtils<>();
        IPage<SysTemplateVersion> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page, SysTemplateVersionVo.class);
    }

    private LambdaQueryWrapper<SysTemplateVersion> getWrapper(SysTemplateVersionQuery query){
        LambdaQueryWrapper<SysTemplateVersion> wrapper = Wrappers.lambdaQuery();
        if(ObjectUtil.isNotEmpty(query.getId())){
            wrapper.eq(SysTemplateVersion::getId,query.getId());
        }
        if(ObjectUtil.isNotEmpty(query.getVersionName())){
            wrapper.eq(SysTemplateVersion::getVersionName,query.getVersionName());
        }
        if(ObjectUtil.isNotEmpty(query.getVersionRemark())){
            wrapper.eq(SysTemplateVersion::getVersionRemark,query.getVersionRemark());
        }
        if(ObjectUtil.isNotEmpty(query.getCreateTime())){
            wrapper.eq(SysTemplateVersion::getCreateTime,query.getCreateTime());
        }
        return wrapper;
    }


    @Override
    public SysTemplateVersionVo detail(SysTemplateVersionQuery query) {
        SysTemplateVersion detail = this.getById(query.getId());
        if (ObjectUtil.isEmpty(detail)) {
        throw new RedstarsException("记录不存在");
        }
        return SysTemplateVersionConvert.INSTANCE.convert(detail);
    }

    @Override
    public List<SysTemplateVersionVo>  list(SysTemplateVersionQuery query){
       LambdaQueryWrapper<SysTemplateVersion> wrapper = getWrapper(query);
       List<SysTemplateVersion> list = list(wrapper);
       return BeanUtil.copyToList(list,SysTemplateVersionVo.class);
    }
}